import cv2
import numpy as np
import random

from pythonProject.open.open_mumu import win_cmd


def screenshot_methods():
    binary_screenshot = adb_shell(['screencap', '-p'])

    binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n')
    img = cv2.imdecode(np.frombuffer(binary_screenshot, np.uint8), cv2.IMREAD_COLOR)

    # cv2.imshow('screenshot_methods', img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    return img


def click_methods(coordinate_wh):

    if coordinate_wh is None:
        return
    # 接收返回的坐标和尺寸
    # 列出坐标和长宽
    else:
        x = coordinate_wh[0]
        y = coordinate_wh[1]
        a = coordinate_wh[2]
        b = coordinate_wh[3]

        # 随机输出坐标
        # 生成 x 方向上的随机整数坐标
        random_x = random.randint(x, x + a)
        # 生成 y 方向上的随机整数坐标
        random_y = random.randint(y, y + b)
        print('随机点击坐标',random_x, random_y)
        adb_shell(['input', 'tap', str(random_x), str(random_y)])
def slide_methods(coordinate_wh):
    pass


def adb_shell(cmd):
    if not isinstance(cmd, str):
        cmd = list(map(str, cmd))
    return win_cmd(['adb', 'shell'] + cmd)
